package br.com.projeto.dao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.projeto.sakila.bean.Rental;

public class RentalDao {

	public long buscarRental(Connection connection){
		long tempoInicial = System.currentTimeMillis();
		
		try {
			PreparedStatement preparedStatement = 
					connection.prepareStatement("select rental_id, last_update, rental_date, return_date from rental");
			
			ResultSet resultSet = preparedStatement.executeQuery();
			List<Rental> lista = new ArrayList<>();
			Rental obj;
			while(resultSet.next()){
				obj = null;
				obj = new Rental();
				obj.setId(Integer.parseInt(resultSet.getString("rental_id")));
				obj.setLastUpdate(resultSet.getDate("last_update"));
				obj.setRentalDate(resultSet.getDate("rental_date"));		
				obj.setReturnDate(resultSet.getDate("return_date"));
				lista.add(obj);
			}
			
			resultSet.close();
			preparedStatement.close();
			
		} catch (SQLException exception) {
			System.err.println("Erro JDBC" + exception);
			throw new RuntimeException(exception);
		}
		
		long tempoFinal = System.currentTimeMillis(); 
		long tempo = tempoFinal - tempoInicial;
		return tempo;
	}
	
}
